<template>
    <div>
        <el-header style="font-size: 12px">
            <el-form inline v-if='isHandle' style="text-align: right; ">
                <el-form-item v-for='item in btnHandle' :key="item.label">
                    <el-button :type="item.type"  @click='item.handle()'>{{item.label}}</el-button>
                </el-form-item>
            </el-form>
            <el-tabs v-model="selectTab" type="border-card" @tab-click="ChangeTab">
                <el-tab-pane
                        v-for="item in tabs"
                        :key="item.name"
                        :label="item.title"
                        :name="item.name">
                    <component :is=item.content
                               :mainData="mainData"
                               :mapAttrs="mapAttrs"
                                @on-mainData-change="ChangeMainData">
                    </component>
                </el-tab-pane>
            </el-tabs>

        </el-header>

    </div>
</template>

<script>
    import MapAttrForm from "../components/mapAttrForm.vue";
    export default {
        name: "EnOnly",
        components:{
            MapAttrForm
        },
        inject:['reload'],
        data() {
            return {
                params:{},//请求传的参数
                frmData:[],//表单数据
                mapData:{},//当前实体属性
                mainData:{},//实体数据
                isHandle:true,//是否显示操作按钮
                btnHandle:[],//工具栏按钮
                isShowTabs:false,//是否显示tab标签
                tabs:[],//tab标签
                selectTab:"1",
                webuser:{},//用户信息
                ur:{},//用户设置信息
                mapAttrs:[],
                rules:{}, //校验规则
                pkVal:""
            }
        },
        created(){
            this.params=this.$store.getters.getData;
            this.webuser = this.$store.getters.getWebUser;
            this.pkVal = this.params.PKVal;
            //获取初始化数据
            var httpHandler = new this.HttpHandler("BP.WF.HttpHandler.WF_CommEntity");
            httpHandler.AddJson(this.params);
            var data = httpHandler.DoMethodReturnString("EntityOnly_Init");
            if(data.indexOf("err@")!=-1){
                this.$message.error(data);
                return;
            }
            this.frmData = JSON.parse(data);
            this.mapData=this.frmData["Sys_MapData"][0];
            this.mainData = this.frmData["MainTable"][0];
            this.mapData = new this.Entity("BP.Sys.MapData", this.mapData);

            //初始化工具栏
            this.InitToolBar();
            this.InitTabs();
            //解析表单信息
            //this.GenerFoolFrm();
        },
        methods:{
            InitToolBar(){
                if(this.params.isTree=="1"&&this.params.isReadonly!="1")
                    this.btnHandle.push({label:'保存',type:'primary',handle:()=>this.Save()});
                else{
                    if (this.mapData.GetPara("IsInsert") == 1)
                        this.btnHandle.push({label:'新建',type:'primary',handle:()=>this.New()});

                    if (this.mapData.GetPara("IsUpdate") == 1)
                        this.btnHandle.push({label:'保存',type:'primary',handle:()=>this.Save()});

                    if (this.mapData.GetPara("IsDelete") == 1 && this.params.PKVal!=null &&this.params.PKVal!=undefined)
                        this.btnHandle.push({label:'删除',type:'primary',handle:()=>this.Delete()});
                }
                if(this.webuser.No=="admin")
                    this.btnHandle.push({label:'设置',type:'primary',handle:()=>this.Setting()});


            },
            InitTabs(){
                var mypk = this.webuser.No + "_Fields_HS_" + this.params.EnName;
                this.ur = new this.Entity("BP.Sys.UserRegedit");
                this.ur.SetPKVal(mypk);
                var count = this.ur.RetrieveFromDBSources();
                var groupFields = this.frmData.Sys_GroupField;
                this.isShowTabs=true;
                groupFields.forEach(gf=>{
                    if (count != 0 && this.ur.MVals.indexOf("," + gf.OID + ",") != -1 && gf.OID != this.mapData.GetPara("PK"))//去除基本信息
                        return true;
                    if(gf.Lab.indexOf(',')==-1){
                        this.selectTab=gf.OID;
                        this.tabs.push({"name":gf.OID,"title":gf.Lab,"content":"MapAttrForm"});
                    }
                    else
                        this.tabs.push({"name":gf.OID,"title":gf.Lab.substring(0,gf.Lab.indexOf(',')),"content":"MapAttrForm"});


                });
                this.ChangeTab(null,null,this.selectTab);



            },
            ChangeTab(tab,event,tabId){

                this.mapAttrs=[];
                //根据tabId设置组件值
                if(tabId==null || tabId==undefined)
                    tabId = tab.name;
                var dtms = this.frmData["dtM"];
                var rm=null;
                var options=[];
                this.frmData.Sys_MapAttr.forEach(attr=>{
                    if (this.ur != null && this.ur != undefined && this.ur.Vals.indexOf("," + attr.KeyOfEn) != -1)
                        return true;
                    if (attr.KeyOfEn == "MyFileNum")
                        return true;
                    if (attr.KeyOfEn == "MyFileName")
                        return true

                    if (attr.GroupID != tabId)
                        return true;
                    rm=null;
                    //判断有无关联的方法
                    dtms.forEach(dtm=>{
                        if (dtm.RefAttrKey == attr.KeyOfEn) {
                            rm = dtm;
                            return false;
                        }
                    });
                    if(rm!=null){
                        attr.IsHaveRm=true;
                        attr.DTM=rm;
                    }else{
                        attr.IsHaveRm=false;
                    }
                    //外键
                    if(attr.UIContralType == 1){
                        //获取外键数据
                        options = this.InitDDLOperation(attr);
                        attr.options=options;
                    }
                    if(attr.UIContralType==2)
                        this.mainData[attr.KeyOfEn] = this.mainData[attr.KeyOfEn]==0?false:true;

                    //校验规则
                    if(attr.UIIsEnable == 1){

                        if(attr.UIIsInput == 1){
                            this.rules[attr.KeyOfEn]=[];
                            this.rules[attr.KeyOfEn].push({ required: true, message: attr.Name+"不能为空"});
                        }
                        if(attr.MyDataType==2){
                            if(this.$.isArray(this.rules[attr.KeyOfEn])==false)
                                this.rules[attr.KeyOfEn]=[];
                            this.rules[attr.KeyOfEn].push({pattern: /^(0|[1-9][0-9]*|-[1-9][0-9]*)$/, message: attr.Name+'必须为整数值', trigger: 'blur'});
                        }
                        if(attr.MyDataType==3 || attr.MyDataType==5){
                            if(this.$.isArray(this.rules[attr.KeyOfEn])==false)
                                this.rules[attr.KeyOfEn]=[];
                            this.rules[attr.KeyOfEn].push({ type: 'double', message: attr.Name+'必须为数值', trigger: 'blur'});
                        }


                    }


                    this.mapAttrs.push(attr);

                });
            },
            ChangeMainData(val){
                this.mainData=val;
            },

            New(){

            },
            Save(){
                var count=0;
                if(this.pkVal!=null){
                    var en = new this.Entity(this.params.EnName,  this.pkVal);
                    for(var key in this.mainData) {
                        if(this.mainData[key]==true)
                            en[key]=1;
                        else if(this.mainData[key]==false)
                            en[key]=0;
                        else
                            en[key] = this.mainData[key];
                    }

                    count = en.Update();
                }else{
                    en = new this.Entity(this.params.EnName);
                    if (this.mainData.No != undefined) {
                        en.SetPKVal(this.mainData.No);
                        //获取当前主键字段是否可以编辑?
                        var val = this.$("#TB_No").prop("disabled");
                        //主键是可以别用户自定义的 zhangsan.
                        if (val == false) {
                            if (en.IsExits() == true) {
                                this.$message.error('编号为[' + en.No + ']的数据已经存在.');
                                return;
                            }
                        }
                    }
                    count = en.Insert();
                    this.pkVal =en.GetPKVal();
                }
                if(count==0)
                    this.$message({"message":"数据保存失败","type":"error"});
                else
                    this.$message("保存成功");
                this.reload();

            },
            Delete(){
                if(this.$confirm('您确定要删除吗？')==false)
                    return;
                //删除相关的扩展设置
                var en = new this.Entity(this.enName,  this.pkVal);
                en.Delete();
                var ens = new this.Entities("BP.Sys.MapExts");
                ens.Delete("FK_MapData",this.enName);

                this.$message({
                    message: '删除成',
                    type: 'success'
                });
                //返回到Search页面
                this.$store.commit('setData', {"EnsName":this.enName+"s"});
                this.$router.push({name:"search"});

            },
            Setting(){

            },
            InitDDLOperation(attr){
                var data = this.frmData[attr.KeyOfEn];
                if (data == undefined)
                    data = this.frmData[attr.UIBindKey];
                if (data == undefined) {
                    data=[];
                    //枚举类型的.
                    if (attr.LGType == 1) {
                        var enums = this.frmData.Sys_Enum;
                        enums.forEach(item=>{
                            if(item.EnumKey==attr.UIBindKey)
                                data.push({"No":item.IntKey,"Name":item.Lab});
                        });
                    }
                }
                return data;
            }
        }
    }
</script>

<style scoped>

</style>